package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.api.admin.IUserClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmNewsArtificialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class WmNewsArtificialServiceImpl extends ServiceImpl<WmNewsMapper,WmNews> implements WmNewsArtificialService {

    private final IUserClient iUserClient;

    @Override
    public PageResponseResult list(NewsAuthDto dto) {
        Page<WmNews> pg = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmNews> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(WmNews::getStatus,dto.getStatus());
        List<WmNews> records = page(pg,lambdaQueryWrapper).getRecords();
        //添加作者姓名
        for (WmNews record : records) {
            Integer id = record.getId();
            //通过feign接口查询姓名
            String userName = iUserClient.findUserName(id);
            record.setAuthorName(userName);
        }
        PageResponseResult result = new PageResponseResult();
        result.setData(records);
        result.setCurrentPage(dto.getPage());
        result.setSize(dto.getSize());
        result.setTotal(records.size());

        return result;
    }
}
